package com.collectplus.express.parcel;

import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.model.Const;
import com.collectplus.express.model.ParcelReceiveWayAndSignedBean;
import com.collectplus.express.parcel.fragment.BaseReceiverParcelFragment;
import com.collectplus.express.parcel.fragment.ParcelReceiveNoPayFragment;

import java.text.MessageFormat;
import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.GlideUtil;
import droid.frame.utils.lang.Str;

/**
 * 收到的包裹通用adapter
 *
 * @author kaka<br>
 *         2017-1-9下午4:33:22
 */
public class ParcelReceiveWayAdapter extends BaseAdapterExt<ParcelReceiveWayAndSignedBean> {

    private String samecityStr = "<font color=#404040>预计</font><font color=#FF001F>{0}</font><font color=#404040>送达，请保持手机畅通</font>";

    private BaseReceiverParcelFragment fragment;

    public ParcelReceiveWayAdapter(ArrayList<ParcelReceiveWayAndSignedBean> items, BaseReceiverParcelFragment fragment) {
        super(items, fragment.getActivityContext());
        this.fragment = fragment;
        setEmptyView(R.layout.parcel_peceive_way_empty);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.parcel_all_receive_item, parent, false);
            holder.parcelNumTV = (TextView) convertView.findViewById(R.id.parcel_num_tv);
            holder.expressLogoIV = (ImageView) convertView.findViewById(R.id.express_logo_iv);
            holder.senderName = (TextView) convertView.findViewById(R.id.parcel_sender_name_tv);
            holder.senderAddress = (TextView) convertView.findViewById(R.id.parcel_sender_address_tv);
            holder.senderPhone = (TextView) convertView.findViewById(R.id.parcel_contract_phone_tv);
            holder.expressInfoTV = (TextView) convertView.findViewById(R.id.parcel_express_info_tv);
            holder.routerInfoTV = (TextView) convertView.findViewById(R.id.parcel_collect_code);
            holder.parcelTypeIV = (ImageView) convertView.findViewById(R.id.city_wide_or_other);
            holder.oprateDetailTV = (TextView) convertView.findViewById(R.id.parcel_detail_tv);
            holder.bottomLineV = convertView.findViewById(R.id.divde_line_v);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ParcelReceiveWayAndSignedBean item = getItem(position);
        holder.senderName.setText("寄件人：" + item.getUserName());
        holder.senderAddress.setText("地址：" + item.getUserAddress());
        holder.senderPhone.setText("联系电话：" + item.getUserPhone());
        holder.parcelNumTV.setText((position + 1) + "");
        GlideUtil.loadImage(context, item.getExpressCompanyUrl(), 0, holder.expressLogoIV);
        //同城
        if (item.getParcelType() == Const.ParcelType.sameCityToHome) {
            holder.parcelNumTV.setBackgroundResource(R.drawable.take_send_samecity_bg_shape);
            holder.parcelNumTV.setTextColor(context.getResources().getColor(R.color.app_font_1));
            holder.parcelTypeIV.setImageResource(R.drawable.take_city_wide_icon);
            holder.routerInfoTV.setText(Html.fromHtml(MessageFormat.format(samecityStr, item.getCurrentRouter())));
        } else {
            holder.parcelNumTV.setBackgroundResource(R.drawable.take_send_bg_shape);
            holder.parcelNumTV.setTextColor(Color.parseColor("#EA5252"));
            holder.parcelTypeIV.setImageResource(R.drawable.take_national_icon);
            holder.routerInfoTV.setText(item.getCurrentRouter());
        }
        if (Str.isNotEmpty(item.getExpressNumber())) {
            holder.expressInfoTV.setText(item.getExpressCompanyName() + "：" + item.getExpressNumber());
        } else {
            holder.expressInfoTV.setText(item.getExpressCompanyName());
        }
        if (position == getItemsSize() - 1) {
            holder.bottomLineV.setVisibility(View.INVISIBLE);
        } else {
            holder.bottomLineV.setVisibility(View.VISIBLE);
        }
        holder.oprateDetailTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item != null) {
                    ((ParcelReceiveNoPayFragment) fragment).getParcelDetail(item.getId());
                }
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView parcelNumTV;
        ImageView expressLogoIV;
        TextView senderName;
        TextView senderAddress;
        TextView senderPhone;
        TextView expressInfoTV;
        TextView routerInfoTV;
        ImageView parcelTypeIV;
        TextView oprateDetailTV;
        View bottomLineV;
    }
}
